﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Model;

namespace GtsServer.Service.Controller
{
    public class LotteryPoolController
    {
        LotteryPoolDAL dal = new LotteryPoolDAL();

        public LotteryPoolResponse EditLotteryPool(LotteryPoolRequest request)
        {
            LotteryPoolResponse response = new LotteryPoolResponse()
            {
                egg = 0,
                status = -1,
                pool = 0,
                ticket = 0,
                power = new LotteryPoolResponsManage()
            };
            response.odds = new ChanceResponse();
            response.odds.model = new List<ChanceModelManage>();
            response.odds.little_pic = new List<List<int>>();
            response.odds.lucky_pic = new List<List<int>>();
            response.odds.start = new List<List<int>>();
            response.odds.end = new List<List<int>>();
            response.odds.sel_high = new List<SelHighManage>();
            response.odds.sel_low = new List<SelLowManage>();
            response.odds.lucky_light = new List<LuckyLightManage>();
            response.odds.cat = new List<CatManage>();
            response.odds.sea = new List<List<int>>();
            response.odds.bar = new List<List<int>>();
            response.odds.plane = new List<CatManage>();
            response.notice = new NoticeGameMainManage();
            response.notice.detail = new List<NoticeGameMainDetailManage>();

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            if (request.pool.bet.ratio > 0)
            {
                //总共押注数
                int totalBet = request.pool.bet.quantity.Sum() * request.pool.bet.ratio;
                //压中的分数  用来兑换彩蛋的
                int totalResult = 0;

                foreach (var item in request.pool.result)
                {
                    //找出压中所对应下标的倍率
                    ItemArrayModel model = ItemArraryValue.ItemList[item];
                    if (model.arrayIndex != -1)
                        totalResult += request.pool.bet.quantity[model.arrayIndex] * request.pool.bet.ratio * model.bet;
                }

                DataSet ds = dal.EditLotteryPool(userAccount, totalBet, totalResult);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    response.egg = Convert.ToInt32(ds.Tables[0].Rows[0]["egg"]);
                    response.ticket = Convert.ToInt32(ds.Tables[0].Rows[0]["ticket"]);
                    response.pool = Convert.ToInt32(ds.Tables[0].Rows[0]["pool"]);
                    response.status = Convert.ToInt32(ds.Tables[0].Rows[0]["status"]);
                    response.power.status = Convert.ToBoolean(ds.Tables[0].Rows[0]["switchStatus"]);
                    response.power.msg = ds.Tables[0].Rows[0]["switchMsg"].ToString();
                    response.ratio = Convert.ToInt32(ds.Tables[0].Rows[0]["ratio"]);

                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        if (item["Name"].ToString().Equals("chance") && !item["EditTime"].ToString().Equals(request.common.odds_id))
                        {
                            ChanceController chanceController = new ChanceController();
                            response.odds = chanceController.GetChance();
                        }
                        else if ((item["Name"].ToString().Equals("notice") && !item["EditTime"].ToString().Equals(request.common.notice_id)))
                        {
                            NoticeDAL noticedal = new NoticeDAL();
                            ds = noticedal.GetListByTaskIDAndUsed(0, Convert.ToInt32(EnumNoticeType.彩池公告));
                            foreach (DataRow notice in ds.Tables[0].Rows)
                            {
                                response.notice.id = item["EditTime"].ToString();
                                NoticeGameMainDetailManage model = new NoticeGameMainDetailManage()
                                {
                                    chars = notice["NoticeContent"].ToString(),
                                    color = new NoticeGameMainColorManage()
                                    {
                                        r = Convert.ToInt32(notice["R"].ToString()),
                                        g = Convert.ToInt32(notice["G"].ToString()),
                                        b = Convert.ToInt32(notice["B"].ToString()),
                                    }
                                };
                                response.notice.detail.Add(model);
                            }
                        }
                    }
                }
            }
            return response;
        }

        public LotteryPoolResponse LotteryPoolInit(LotteryPoolInitRequest request)
        {
            LotteryPoolResponse response = new LotteryPoolResponse()
            {
                egg = 0,
                status = -1,
                pool = 0,
                ticket = 0,
                power = new LotteryPoolResponsManage(),
            };

            response.odds = new ChanceResponse();
            response.odds.model = new List<ChanceModelManage>();
            response.odds.little_pic = new List<List<int>>();
            response.odds.lucky_pic = new List<List<int>>();
            response.odds.start = new List<List<int>>();
            response.odds.end = new List<List<int>>();
            response.odds.sel_high = new List<SelHighManage>();
            response.odds.sel_low = new List<SelLowManage>();
            response.odds.lucky_light = new List<LuckyLightManage>();
            response.odds.cat = new List<CatManage>();
            response.odds.sea = new List<List<int>>();
            response.odds.bar = new List<List<int>>();
            response.odds.plane = new List<CatManage>();
            response.notice = new NoticeGameMainManage();
            response.notice.detail = new List<NoticeGameMainDetailManage>();
            response.cheat = false;

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = dal.LotteryPoolInit(userAccount);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                response.egg = Convert.ToInt32(ds.Tables[0].Rows[0]["egg"]);
                response.ticket = Convert.ToInt32(ds.Tables[0].Rows[0]["ticket"]);
                response.pool = Convert.ToInt32(ds.Tables[0].Rows[0]["pool"]);
                response.status = 0;
                response.power.status = Convert.ToBoolean(ds.Tables[0].Rows[0]["status"]);
                response.power.msg = ds.Tables[0].Rows[0]["msg"].ToString();
                response.cheat = response.cheat = Convert.ToInt32(ds.Tables[0].Rows[0]["state"]) == 1 ? false : true;

                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    if (item["Name"].ToString().Equals("chance") && !item["EditTime"].ToString().Equals(request.common.odds_id))
                    {
                        ChanceController chanceController = new ChanceController();
                        response.odds = chanceController.GetChance();
                    }
                    else if ((item["Name"].ToString().Equals("notice") && !item["EditTime"].ToString().Equals(request.common.notice_id)))
                    {
                        NoticeDAL noticedal = new NoticeDAL();
                        ds = noticedal.GetListByTaskIDAndUsed(0, Convert.ToInt32(EnumNoticeType.彩池公告));
                        foreach (DataRow notice in ds.Tables[0].Rows)
                        {
                            response.notice.id = item["EditTime"].ToString();
                            NoticeGameMainDetailManage model = new NoticeGameMainDetailManage()
                            {
                                chars = notice["NoticeContent"].ToString(),
                                color = new NoticeGameMainColorManage()
                                {
                                    r = Convert.ToInt32(notice["R"].ToString()),
                                    g = Convert.ToInt32(notice["G"].ToString()),
                                    b = Convert.ToInt32(notice["B"].ToString()),
                                }
                            };
                            response.notice.detail.Add(model);
                        }
                    }
                }
            }

            return response;
        }

        /// <summary>
        /// 彩池排行  （从排行  根据个人彩蛋数排行）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public MatchRankResponse LotteryPoolRank(LotteryPoolRankRequest request)
        {
            MatchRankResponse response = new MatchRankResponse()
            {
                rank = new List<MatchRankManage>()
            };
            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = dal.LotteryPoolRank(userAccount);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    MatchRankManage model = new MatchRankManage()
                    {
                        num = item["num"].ToString(),
                        name = item["name"].ToString(),
                        fraction = item["fraction"].ToString(),
                        myself = Convert.ToInt32(item["myself"])
                    };
                    response.rank.Add(model);
                }
            }

            return response;
        }

        public LotteryPoolSizeResponse LotteryPoolGuess(LotteryPoolSizeRequest request)
        {
            LotteryPoolSizeResponse response = new LotteryPoolSizeResponse()
            {
                egg = 0,
                pool = 0,
                guess = new LotteryPoolSizeResponseManage()
                {
                    status = false,
                    nub = 1
                }
            };

            Random rd = new Random();
            int value = rd.Next(100);

            if (value < 30)
            {
                response.guess.status = true;
                //压小
                if (request.guess.size == 0)
                    response.guess.nub = rd.Next(1, 10);
                else
                    response.guess.nub = rd.Next(11, 20);
            }
            else
            {
                response.guess.status = false;
                //压小
                if (request.guess.size == 0)
                    response.guess.nub = rd.Next(11, 20);
                else
                    response.guess.nub = rd.Next(1, 10);
            }

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            if (request.guess.capital > 0)
            {
                DataSet ds = dal.LotteryPoolGuess(userAccount, request.guess.capital, response.guess.status ? 1 : 0);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    int eggNum = ds.Tables[0].Rows[0]["egg"] == null ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["egg"]);
                    int poolNum = ds.Tables[0].Rows[0]["pool"] == null ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["pool"]);
                    response.egg = eggNum;
                    response.pool = poolNum;
                }
            }

            return response;
        }
    }
}
